package com.linq.cool.beauty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.linq.cool.api.domain.BeautyMember;
import com.linq.cool.api.domain.BeautyMemberItem;
import com.linq.cool.beauty.config.BaiduMapProperties;
import com.linq.cool.beauty.domain.BeautyOrder;
import com.linq.cool.beauty.domain.BeautyTopUp;
import com.linq.cool.beauty.enums.OrderStateEnum;
import com.linq.cool.beauty.service.*;
import com.linq.cool.beauty.vo.statistical.PeopleNumberVO;
import com.linq.cool.beauty.vo.statistical.StatisticalVO;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/2/4 10:50
 * @Description: 统计使用
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("statistical")
public class StatisticalController extends BaseController {
    @Autowired
    private BeautyTopUpService beautyTopUpService;
    @Autowired
    private BeautyRefundService beautyRefundService;
    @Autowired
    private BaiduMapProperties baiduMapProperties;
    @Autowired
    private BeautyMemberService beautyMemberService;
    @Autowired
    private BeautyMemberItemService beautyMemberItemService;
    @Autowired
    private BeautyOrderService beautyOrderService;

    /**
     * 查询近6个月的月份的收入统计
     *
     * @return
     */
    @GetMapping("monthlyIncome")
    public Result<?> monthlyIncome() {
        return Result.ok(beautyTopUpService.statisticalMonthlyIncome());
    }

    /**
     * 查询近6个月的月份的退费统计
     *
     * @return
     */
    @GetMapping("monthlyRefund")
    public Result<?> monthlyRefund() {
        return Result.ok(beautyRefundService.statisticalMonthlyRefund());
    }

    /**
     * 人数统计
     *
     * @return
     */
    @GetMapping("peopleNumber")
    public Result<?> peopleNumber() {
        int bindedMember = beautyMemberService.count(new LambdaQueryWrapper<BeautyMember>()
                                                             .eq(BeautyMember::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                             .isNotNull(BeautyMember::getOpenId)
                                                             .isNotNull(BeautyMember::getPhone)
        );
        int totalCount = beautyMemberService.count(new LambdaQueryWrapper<BeautyMember>()
                                                           .eq(BeautyMember::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
        );
        return Result.ok(new PeopleNumberVO().setTotalCount(totalCount).setBindWechatCount(bindedMember));
    }


    /**
     * 统计客户的总消费金额(不包含退费的),总退费金额
     */
    @GetMapping("memberConsumption/{memberId}")
    public Result<?> memberConsumption(@PathVariable("memberId") Long memberId) {
        List<StatisticalVO> list = new LinkedList<>();
        BeautyMemberItem memberItem = beautyMemberItemService.getOne(new LambdaQueryWrapper<BeautyMemberItem>()
                                                                             .eq(BeautyMemberItem::getMemberId, memberId)
        );
        StatisticalVO topUpAmountVO = new StatisticalVO();
        StatisticalVO refundAmountVO = new StatisticalVO();
        StatisticalVO consumeAmountVO = new StatisticalVO();
        topUpAmountVO.setName("累计充值");
        refundAmountVO.setName("累计退费");
        consumeAmountVO.setName("累计消费");
        if (null == memberItem) {
            topUpAmountVO.setValue(0);
            refundAmountVO.setValue(0);
            consumeAmountVO.setValue(0);
        } else {
            topUpAmountVO.setValue(memberItem.getTopUpAmount());
            refundAmountVO.setValue(memberItem.getRefundAmount());
            consumeAmountVO.setValue(memberItem.getConsumeAmount());
        }
        list.add(refundAmountVO);
        list.add(topUpAmountVO);
        list.add(consumeAmountVO);
        return Result.ok(list);
    }

    /**
     * 统计客户的 充值次数,退费次数
     */
    @GetMapping("consumptionCount/{memberId}")
    public Result<?> consumptionCount(@PathVariable("memberId") Long memberId) {
        List<StatisticalVO> list = new LinkedList<>();
        BeautyMemberItem memberItem = beautyMemberItemService.getOne(new LambdaQueryWrapper<BeautyMemberItem>()
                                                                             .eq(BeautyMemberItem::getMemberId, memberId)
        );
        StatisticalVO topUpCountVO = new StatisticalVO();
        StatisticalVO refundCountVO = new StatisticalVO();
        topUpCountVO.setName("充值");
        refundCountVO.setName("退费");
        if (null == memberItem) {
            topUpCountVO.setValue(0);
            refundCountVO.setValue(0);
        } else {
            topUpCountVO.setValue(memberItem.getTopUpCount());
            refundCountVO.setValue(memberItem.getRefundCount());
        }
        list.add(topUpCountVO);
        list.add(refundCountVO);
        return Result.ok(list);
    }

    /**
     * 统计客户的,支付宝使用次数,现金使用次数
     */
    @GetMapping("paymentCount/{memberId}")
    public Result<?> paymentCount(@PathVariable("memberId") Long memberId) {
        List<StatisticalVO> list = new LinkedList<>();
        int alipayCount = beautyTopUpService.count(new LambdaQueryWrapper<BeautyTopUp>()
                                                           .eq(BeautyTopUp::getPayType, UserConstants.StateEnum.PAY_TYPE_ALIPAY.getCode())
                                                           .eq(BeautyTopUp::getMemberId, memberId)
        );
        StatisticalVO alipayCountVO = new StatisticalVO();
        alipayCountVO.setName("支付宝").setValue(alipayCount);
        list.add(alipayCountVO);

        int cashCount = beautyTopUpService.count(new LambdaQueryWrapper<BeautyTopUp>()
                                                         .eq(BeautyTopUp::getPayType, UserConstants.StateEnum.PAY_TYPE_CASH.getCode())
                                                         .eq(BeautyTopUp::getMemberId, memberId)
        );
        StatisticalVO cashCountVO = new StatisticalVO();
        cashCountVO.setName("现金").setValue(cashCount);
        list.add(cashCountVO);
        return Result.ok(list);
    }


    /**
     * 统计消费榜前10
     */
    @GetMapping("topTenSpenders")
    public Result<?> topTenSpenders() {
        return Result.ok(beautyTopUpService.statisticalTopTenSpenders());
    }

    /**
     * 订单统计
     * 根据客户id查询出当前每个订单状态下面的数量
     * 订单状态(0:待付款 1:待确认 2:待评价 3:已完成 4:无效订单 5已关闭
     *
     * @param memberId
     *
     * @return
     */
    @GetMapping("statisticMemberOrder/{memberId}")
    public Result<?> statisticMemberOrder(@PathVariable("memberId") Long memberId) {
        List<StatisticalVO> list = new LinkedList<>();
        int waitPayCount = beautyOrderService.count(new LambdaQueryWrapper<BeautyOrder>()
                                                            .eq(BeautyOrder::getStatus, OrderStateEnum.WAIT_PAY.getCode())
                                                            .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                            .eq(BeautyOrder::getMemberId, memberId)
        );
        int waitConfirmSignCount = beautyOrderService.count(new LambdaQueryWrapper<BeautyOrder>()
                                                                    .eq(BeautyOrder::getStatus, OrderStateEnum.WAIT_CONFIRM.getCode())
                                                                    .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                                    .eq(BeautyOrder::getMemberId, memberId)
        );
        int alreaddySignCount = beautyOrderService.count(new LambdaQueryWrapper<BeautyOrder>()
                                                                 .eq(BeautyOrder::getStatus, OrderStateEnum.ALREADY_SIGN.getCode())
                                                                 .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                                 .eq(BeautyOrder::getMemberId, memberId)
        );
        int finishCount = beautyOrderService.count(new LambdaQueryWrapper<BeautyOrder>()
                                                           .eq(BeautyOrder::getStatus, OrderStateEnum.FINISH.getCode())
                                                           .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                           .eq(BeautyOrder::getMemberId, memberId)
        );
        int invalidCount = beautyOrderService.count(new LambdaQueryWrapper<BeautyOrder>()
                                                            .eq(BeautyOrder::getStatus, OrderStateEnum.INVALID.getCode())
                                                            .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                            .eq(BeautyOrder::getMemberId, memberId)
        );
        int closeCount = beautyOrderService.count(new LambdaQueryWrapper<BeautyOrder>()
                                                          .eq(BeautyOrder::getStatus, OrderStateEnum.CLOSED.getCode())
                                                          .eq(BeautyOrder::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
                                                          .eq(BeautyOrder::getMemberId, memberId)
        );
        /**
         *  WAIT_PAY(0, "待付款"),
         *  WAIT_CONFIRM(1, "待确认"),
         *  ALREADY_SIGN(2, "待评价"),
         *  FINISH(3, "已完成"),
         *  INVALID(4, "无效订单"),
         *  CLOSED(5, "已关闭"),
         */
        StatisticalVO waitPayVO = new StatisticalVO();
        StatisticalVO waitConfirmVO = new StatisticalVO();
        StatisticalVO alreaddySignVO = new StatisticalVO();
        StatisticalVO finishVO = new StatisticalVO();
        StatisticalVO invalidVO = new StatisticalVO();
        StatisticalVO closeVO = new StatisticalVO();
        waitPayVO.setName("待付款").setValue(waitPayCount);
        waitConfirmVO.setName("待确认").setValue(waitConfirmSignCount);
        alreaddySignVO.setName("待评价").setValue(alreaddySignCount);
        finishVO.setName("已完成").setValue(finishCount);
        invalidVO.setName("无效订单").setValue(invalidCount);
        closeVO.setName("已关闭").setValue(closeCount);
        list.add(waitPayVO);
        list.add(waitConfirmVO);
        list.add(alreaddySignVO);
        list.add(finishVO);
        list.add(invalidVO);
        list.add(closeVO);
        return Result.ok(list);
    }

    /**
     * 根据客户id查询出当前客户的最近6月份的充值情况
     *
     * @param memberId
     *
     * @return
     */
    @GetMapping("statisticMemberTopUp/{memberId}")
    public Result<?> statisticMemberTopUp(@PathVariable("memberId") Long memberId) {
        return Result.ok(beautyTopUpService.statisticMemberTopUp(memberId));
    }

    /**
     * 客户月统计订单
     *
     * @return
     */
    @GetMapping("statisticMemberMonthOrder/{memberId}")
    public Result<?> statisticMemberMonthOrder(@PathVariable("memberId") Long memberId) {
        return Result.ok(beautyOrderService.statisticMemberMonthOrder(memberId));
    }

    /**
     * 月订单统计
     *
     * @return
     */
    @GetMapping("statisticMonthOrder")
    public Result<?> statisticMonthOrder() {
        return Result.ok(beautyOrderService.statisticMonthOrder());
    }

    //    /**
    //     * 获取人口分布
    //     *
    //     * @return
    //     */
    //    @GetMapping("populationDistribution")
    //    public R<?> populationDistribution() throws IOException {
    //        List<BeautyCustomer> customerList = beautyCustomerService.list(new LambdaQueryWrapper<BeautyCustomer>()
    //                                                                               .eq(BeautyCustomer::getIsDeleted, UserConstants.StateEnum.DELETE_FLAG_EXIST.getCode())
    //        );
    //        List<String> result = new ArrayList<>();
    //        for (BeautyCustomer customer : customerList) {
    //            String dimensionBody = HttpUtils.get(baiduMapProperties.getUrl(customer.getProvince() + customer.getCity())).body();
    //            BaduMapResult baduMapResult = JSON.parseObject(dimensionBody, BaduMapResult.class);
    //
    //        }
    //
    //        return R.ok();
    //    }
}
